package ch9;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

public class XMLSearch {
	public static void main(String[] args) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new File("src/ch9/clothes.xml"));
		Scanner in = new Scanner(System.in);
		// genju ping pai chazhao shangpin
		System.out.println("请输入品牌或者产地：");
		String brand = in.next();
		List<Element> list = doc.selectNodes("/clothes/cloth[brand='" + brand + "' or @chandi='" + brand + "']");
		if (list.size() == 0) {
			System.out.println("没有找到");
			return;
		}
		for (Element e : list) {
			String chandi = e.attributeValue("chandi");
			String yanse = e.attributeValue("color");
			// Element brand = (Element) e.selectNodes("brand").get(0);
			Element bnr = (Element) e.selectSingleNode("brand");
			String b = bnr.getText();
			String price = e.selectSingleNode("price").getText();
			double jiage = Double.parseDouble(price);
			Element su = (Element) e.selectSingleNode("suit");
			String suit = su == null ? "" : su.getText();

			System.out.println(b + "," + jiage + ",适合" + suit + ",产地：" + chandi + ",颜色：" + yanse);
			System.out.println(">>>>>>>>>>>>>>");
		}
	}
}
